﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string WH(maintain m, int ACID);
    }
    public partial class Service : IService
    {
        public string WH(maintain m, int ACID)
        { 
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        string r2 = maintain_Add(m,ACID, ctx);
                        if (!string.IsNullOrEmpty(r2))
                        {
                            throw new Exception(r2);
                        }
                        foreach (maintian_D d in m.maintian_D)
                        {
                            numberpool phone = ctx.numberpool.Single(p=>p.phone==m.phone);
                            if (d.tid==1)
                            {
                                phone.state=2;
                            }
                            if (d.tid==2)
                            {
                                phone.state = 1;
                            }
                            if (d.tid ==3)
                            {
                                if (string.IsNullOrWhiteSpace(m.opencardpid))
                                {
                                    ctx.Cards.Single(p => p.cno == phone.cno).state = -1;
                                    phone.cno = d.newcno;
                                }
                            }
                            if (d.tid==4)
                            {
                                phone.state = 3;
                            }
                            if (d.tid==5)
                            {
                                phone.state = 1;
                            }
                            if (d.tid==6)
                            {
                                phone.pwd = d.newpwd;
                            }
                            if (d.tid==7)
                            {
                                phone.linktel = d.linktel;
                            }
                            if (d.tid==8)
                            {
                                phone.htno = d.newhtno;
                                phone.username = d.newusername;
                                phone.userIDcard = d.newuserIDcard;
                            }
                            ctx.maintian_D.InsertOnSubmit(d);

                        }
                        RecordBalance(m.S_total, m.clientID, ctx, ACID);
                        RecordBalance(m.F_total, m.gysID, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }
        

    }


}

